package com.anjho.bean;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.persistence.NoResultException;

import org.primefaces.event.SelectEvent;

import com.anjho.dao.DAOClient;
import com.anjho.dao.DAOProduct;
import com.anjho.dao.DAOSale;
import com.anjho.pojo.Client;
import com.anjho.pojo.Product;
import com.anjho.pojo.Sale;
import com.anjho.pojo.User;
import com.anjho.utils.Constants;

@ManagedBean(name = "saleBean")
@ViewScoped
public class SaleBean extends GenericBean {

	private static final long serialVersionUID = 7208611040506239884L;

	private List<Product> productList;
	private Product product;
	private String barcode;
	private String proNumber;
	private Sale sale;
	private Client client;
	private boolean allowSelectClient;
	private BigDecimal total;
	private Product prodAuxTbl;

	public SaleBean() {
		super();
		this.clearForm();
	}

	public void clearForm() {
		this.productList = new ArrayList<Product>();
		this.product = new Product();
		this.prodAuxTbl = new Product();
		this.allowSelectClient = false;
		this.total = new BigDecimal(0);
		this.barcode = "";
		this.proNumber = "";
		try {
			DAOClient daoClient = new DAOClient();
			this.client = (Client) daoClient.getObjectByParameter(
					new String[] { "clcode" }, new Object[] { 1 },
					"Client.getClientByClcode");
			this.sale = new Sale();
		} catch (Exception e) {
			FacesMessage facesMsg = new FacesMessage(
					FacesMessage.SEVERITY_FATAL, "Erro", e.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}

	public List<Product> getProductList() {
		return productList;
	}

	public void setProductList(List<Product> productList) {
		this.productList = productList;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public String getBarcode() {
		return barcode;
	}

	public void setBarcode(String barcode) {
		this.barcode = barcode;
	}

	public String getProNumber() {
		return proNumber;
	}

	public void setProNumber(String proNumber) {
		this.proNumber = proNumber;
	}

	public Sale getSale() {
		return sale;
	}

	public void setSale(Sale sale) {
		this.sale = sale;
	}

	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}

	public boolean isAllowSelectClient() {
		return allowSelectClient;
	}

	public void setAllowSelectClient(boolean allowSelectClient) {
		this.allowSelectClient = allowSelectClient;
	}

	public BigDecimal getTotal() {
		return total;
	}

	public void setTotal(BigDecimal total) {
		this.total = total;
	}

	public Product getProdAuxTbl() {
		return prodAuxTbl;
	}

	public void setProdAuxTbl(Product prodAuxTbl) {
		this.prodAuxTbl = prodAuxTbl;
	}

	public void onTxtProductBarcodeBlur() throws Exception {
		try {
			DAOProduct daoProduct = new DAOProduct();
			this.product = daoProduct.getProductByBarcode(this.barcode);
			daoProduct = null;
		} catch (NoResultException ex) {
			this.product = new Product();
		} catch (Exception ex) {
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Erro", ex.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}

	public void acClientSelect(SelectEvent event) {
		this.sale.setClient(this.client);
	}

	public void onAddToSaleList() {
		try {
			this.productList.add(this.product);
			this.total = new BigDecimal(0);
			for (Product prod : this.productList) {
				this.total = this.total.add(prod.getPrice());
			}
		} catch (Exception ex) {
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Erro desconhecido", "Houve um erro não identificado.");
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}

	public void onRemoveFromSaleList() {
		try {
			this.productList.remove(this.prodAuxTbl);
			this.total = new BigDecimal(0);
			for (Product prod : this.productList) {
				this.total = this.total.add(prod.getPrice());
			}
		} catch (Exception ex) {
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Erro desconhecido", "Houve um erro não identificado.");
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}

	public void onCloseSale() {
		try {
			this.sale.setClient(this.client);
			this.sale.setUser((User) super.getFromSession(Constants.LOGGED_USER));
			this.sale.setValue(this.total);
			this.sale.setSaleDate(new Date());
			this.sale.setProductList(this.productList);

			DAOSale daoSale = new DAOSale();
			daoSale.insert(this.sale);
			daoSale = null;
			
			//TODO : Corrigir assim que surgir uma melhor forma de fazer
			DAOProduct daoProduct = new DAOProduct();
			for (Product pro : this.sale.getProductList()){
				pro.setQtd(pro.getQtd()-1);
				daoProduct.update(pro);
			}
			daoProduct = null;
			
			this.clearForm();
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Sucesso", "Venda efetuada com sucesso!");
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		} catch (Exception ex) {
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Erro desconhecido", "Houve um erro não identificado.");
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}

}
